/*
 * @Author: MarioGo
 * @Date: 2021-11-10 09:46:17
 * @LastEditTime: 2021-11-20 16:39:31
 * @LastEditors: MarioGo
 * @Description: 文件描述
 * @FilePath: /zheye/src/router/index.ts
 * 可以输入预定的版权声明、个性签名、空行等
 */
import { createRouter, createWebHashHistory } from 'vue-router';
import store from '@/store';
// import axios from 'axios';

const routerHistory = createWebHashHistory();

const router = createRouter({
  history: routerHistory,
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import(/* webpackChunkName:'Home' */ '@/views/Home.vue'),
    },
    {
      path: '/login',
      name: 'login',
      component: () => import(/* webpackChunkName:'Login' */ '@/views/Login.vue'),
      meta: { redireactAlreadyLogin: true },
    },
    {
      path: '/signup',
      name: 'signup',
      component: () => import(/* webpackChunkName:'SignUp' */ '@/views/SignUp.vue'),
      meta: { redireactAlreadyLogin: true },
    },
    {
      path: '/posts/:id',
      name: 'post',
      component: () => import(/* webpackChunkName:'PostDatail' */ '@/views/PostDetail.vue'),
    },
    {
      path: '/column/:id',
      name: 'columnDetail',
      component: () => import(/* webpackChunkName:'columnDetail' */ '@/views/ColumnDetail.vue'),
    },
    {
      path: '/create',
      name: 'CreatePost',
      component: () => import(/* webpackChunkName:'CreatePost' */ '@/views/CreatePost.vue'),
      meta: { requiredLogin: true },
    },
    {
      path: '/edit',
      name: 'edit',
      component: () => import(/* webpackChunkName:'CreatePost' */ '@/views/EditProfile.vue'),
      meta: { requiredLogin: true },
    },
  ],
});
//路由权限
router.beforeEach((to, from, next) => {
  console.log('%c 🍯 from: ', 'font-size:20px;background-color: #ED9EC7;color:#fff;', from);
  console.log('%c 🍝 to: ', 'font-size:20px;background-color: #4b4b4b;color:#fff;', to);
  const { user, token } = store.state;
  console.log('%c 🍈 user: ', 'font-size:20px;background-color: #7F2B82;color:#fff;', user.isLogin);
  console.log('%c 🥤 token: ', 'font-size:20px;background-color: #33A5FF;color:#fff;', token);
  const { requiredLogin, redirectAlreadyLogin } = to.meta;
  console.log(
    '%c 🍎 requiredLogin: ',
    'font-size:20px;background-color: #6EC1C2;color:#fff;',
    requiredLogin,
  );
  if (!user.isLogin) {
    if (token) {
      next();
      //  如果token存在去验证token是否有效获取登录信息
      // axios.defaults.headers.common.Authorization = `Bearer ${token}`;
      // store
      //   .dispatch('fetchCurrentUser')
      //   .then(() => {
      //     if (redirectAlreadyLogin) {
      //       next('/');
      //     } else {
      //       next();
      //     }
      //   })
      //   .catch((e) => {
      //     console.error(e);
      //     localStorage.removeItem('token');
      //     next('login');
      //   });
    } else {
      if (requiredLogin) {
        next('login');
      } else {
        next();
      }
    }
  } else {
    if (redirectAlreadyLogin) {
      next('/');
    } else {
      next();
    }
  }
});

export default router;
